﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Common;

public partial class System_User_Add : System.Web.UI.Page
{
    protected int UserKey;

    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["menutype"]) ? "" : Request["menutype"].ToString(); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        UserKey = string.IsNullOrEmpty(Request["UserKey"]) ? 0 : Convert.ToInt32(Request["UserKey"]);

        if (!this.IsPostBack)
        {
            txt_UserType.DataSource = new Sys_Para().SelectAccountType();
            txt_UserType.DataTextField = "TypeName";
            txt_UserType.DataValueField = "TypeKey";
            txt_UserType.DataBind();
            txt_UserType.Items[0].Selected = true;

            if (UserKey != 0)
            {
                txt_Account.ReadOnly = true;

                Sys_Account account = new Account().GetAccountByKey(UserKey);
                txt_UserType.SelectedValue = account.UserType.ToString();
                txt_Account.Text = account.Account;
                txt_Email.Text = account.Email;
                txt_CompanyName.Text = account.CompanyName;
                txt_ShortCompanyName.Text = account.ShortCompanyName;
                txt_Tel.Text = account.Tel;
                txt_Tel2.Text = account.Tel2;
                txt_FAX.Text = account.FAX;
                txt_RealName.Text = account.RealName;
                txt_Mob.Text = account.Mob;
                txt_Zhiwu.Text = account.Zhiwu;
                txt_CustomerType.Text = account.CustomerType;
                txt_XiShu.Text = account.XiShu.HasValue ? account.XiShu.ToString() : "";
                txt_ContractNO.Text = account.ContractNO;
                txt_Address.Text = account.Address;

                RequiredFieldValidator2.Enabled = false;
                RequiredFieldValidator3.Enabled = false;

                btnSubmit.Text = "修 改";
            }
            
       }
    }

    //添加
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int? num = null;

        if (UserKey == 0)
        {
            if (new Account().IsAccountExist(txt_Account.Text.Trim()))
            {
                WebUtility.JsAlertToBack("用户名重复,请重新填写");
                Response.End();
            }

            
            Sys_Account account = new Sys_Account
            {
                UserType = Convert.ToInt32(txt_UserType.SelectedValue),
                Account = txt_Account.Text.Trim(),
                Password = WebUtility.MD5(txt_Password.Text.Trim(),32),
                Email = txt_Email.Text.Trim(),
                CompanyName = txt_CompanyName.Text.Trim(),
                ShortCompanyName = txt_ShortCompanyName.Text.Trim(),
                Tel = txt_Tel.Text.Trim(),
                Tel2 = txt_Tel2.Text.Trim(),
                FAX = txt_FAX.Text.Trim(),
                RealName = txt_RealName.Text.Trim(),
                Mob = txt_Mob.Text.Trim(),
                Zhiwu = txt_Zhiwu.Text.Trim(),
                CustomerType = txt_CustomerType.Text.Trim(),
                XiShu = txt_XiShu.Text.Trim()==""?num:Convert.ToInt32(txt_XiShu.Text.Trim()),
                ContractNO = txt_ContractNO.Text.Trim(),
                Address = txt_Address.Text.Trim(),
                CreateTime = DateTime.Now,
                State = 0
            };

            if (new Account().AddAccount(account))
            {
                WebUtility.JsAlertToGo("添加用户成功", "User_Add.aspx?menutype=611");
            }
        }
        else
        {
            Sys_Account account = new Sys_Account
            {
                UserKey = UserKey,
                UserType = Convert.ToInt32(txt_UserType.SelectedValue),
                //Account = txt_Account.Text.Trim(),
                Password = string.IsNullOrEmpty(txt_Password.Text.Trim()) ? (new Account().GetAccountByKey(UserKey).Password) : WebUtility.MD5(txt_Password.Text.Trim(), 32),
                Email = txt_Email.Text.Trim(),
                CompanyName = txt_CompanyName.Text.Trim(),
                ShortCompanyName = txt_ShortCompanyName.Text.Trim(),
                Tel = txt_Tel.Text.Trim(),
                Tel2 = txt_Tel2.Text.Trim(),
                FAX = txt_FAX.Text.Trim(),
                RealName = txt_RealName.Text.Trim(),
                Mob = txt_Mob.Text.Trim(),
                Zhiwu = txt_Zhiwu.Text.Trim(),
                CustomerType = txt_CustomerType.Text.Trim(),
                XiShu = txt_XiShu.Text.Trim() == "" ? num : Convert.ToInt32(txt_XiShu.Text.Trim()),
                ContractNO = txt_ContractNO.Text.Trim(),
                Address = txt_Address.Text.Trim()
            };

            if (new Account().EditAccount(account))
            {
                WebUtility.JsAlertToGo("修改用户成功", "User.aspx?menutype=622");
            }
        }
    }

}
